#ifndef TIMER_H
#define TIMER_H

#include "main_inc.h"
#include "clog.h"
#include "SDL.h"

#include "FastDelegate.h"

typedef fastdelegate::FastDelegate0<void > callbackV1;

#define STATE_RUNNING   0x01
#define STATE_PAUSED    0x02
#define STATE_NONINIT   0x00

#define TYP_PERIOD      0x01
#define TYP_ONCE        0x02


class Timer
{
    public:
        Timer();
        virtual ~Timer();

        i32 init();
        i32 start();
        i32 pause();
        i32 unpause();
        i32 reset();
        i32 getTime();      // returns counted time in ms
        i32 setInterval(i32 interval){ _interval = interval; return 1;}

        i32 doOnce(i32 time_ms, callbackV1 call );
        i32 doPeriodicaly(i32 time_ms, callbackV1 call );

    protected:

        callbackV1 _call;           // function to call void func(void* param)
        i32 _type_of_call;          // one call, or periodicaly
        i32 _interval;
        i32 _ticks;                 //
        i32 _counted;               // time from start
        i32 _state;                 // noninit, runiing, paused
        i32 _flag_call;             // are we calling something ?
        i32 _next_call;             // when we should call func ?

        u32 _ID;

    // timer_server_stuff:

    protected:

        static vector<Timer*> _timers;
        static u32 _next_id;

        i32 registerThisTimer();
        i32 unregisterThisTimer();
    public:
        static i32 checkAndRun();


    private:
};

#endif // TIMER_H
